"""
Case Type   : ustore_DQL_LazyAgg
Case Name   : 从本地向 date_dim 表中导入数据
Create At   : 2022-05-10
Owner       : lonely-dance
Description :
    1、使用copy命令插入数据
    2、环境清理
Expect      :
    1、插入数据成功
    2、清理成功
History     :
"""

import os
import subprocess
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class LazyAgg(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----- {os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_root = Node('PrimaryRoot')
        self.pri_user = Node('PrimaryDbUser')
        self.com = Common()
        self.constant = Constant()
        self.t_name1 = 'date_dim'
        self.copy_file1 = 'date_dim.txt'
        self.cur_path1 = os.path.join(macro.SCRIPTS_PATH, self.copy_file1)
        self.parent_path = os.path.dirname(macro.DB_INSTANCE_PATH)
        self.target_path1 = os.path.join(self.parent_path, self.copy_file1)

    def test_lazy(self):
        step_txt = '-----step1:使用copy命令插入数据，expect: 数据插入成功-----'
        self.log.info(step_txt)
        text = '-----step1.1: 获取当前主机的IP地址;expect: 成功-----'
        self.log.info(text)
        cmd = "ifconfig -a|grep inet6 -a2|grep broadcast|awk '{print $2}'"
        self.log.info(cmd)
        self.cur_ip = subprocess.getoutput(cmd).strip()
        self.log.info(self.cur_ip)

        text = '-----step1.2: 复制文件到数据库主机对应目录 expect: 成功-----'
        self.log.info(text)
        if self.pri_user.db_host in self.cur_ip:
            self.log.info('当前主机是数据库主机，无需远程复制')
            cmd = f"cp {self.cur_path1} {self.parent_path};"
            self.log.info(cmd)
            res = self.pri_sh.node.sh(cmd).result()
            self.log.info(res)
        else:
            self.com.scp_file(self.pri_user, self.copy_file1,
                              self.parent_path)

        text = '-----step1.3: 查看数据库主机目标路径下文件 expect: 成功-----'
        self.log.info(text)
        cmd = f"ls -l {self.parent_path}| grep -E '{self.copy_file1}'"
        self.log.info(cmd)
        res = self.pri_root.sh(cmd).result()
        self.log.info(res)
        self.assertNotIn('No such file or directory', res, '文件拷贝失败')
        self.assertIn(f'{self.copy_file1}', res, '文件拷贝失败')

        text = '-----step1.4:使用copy命令插入数据，expect: 数据插入成功-----'
        self.log.info(text)
        self.log.info('---------------使用copy命令插入数据------------------')
        copy_sql = f'''source {macro.DB_ENV_PATH};
            gsql -d {self.pri_user.db_name} -p {self.pri_user.db_port} \
            -U {self.pri_user.db_user} -W {self.pri_user.db_password} \
            -c "\\copy {self.t_name1} from '{self.target_path1}';";'''
                   
        self.log.info(copy_sql)
        copy_result = self.pri_user.sh(copy_sql).result()
        self.log.info(copy_result)
        self.assertEqual('',copy_result, '执行失败:' + step_txt)
        self.log.info('---------------查看插入数据------------------')
        sql = f'''source {macro.DB_ENV_PATH};
            gsql -d {self.pri_user.db_name} -p {self.pri_user.db_port} \
            -U {self.pri_user.db_user} -W {self.pri_user.db_password} \
            -c "select count(*) from {self.t_name1};"'''
        self.log.info(sql)
        res = self.pri_user.sh(sql).result()
        self.log.info(res)
        self.assertIn('63', res, '文件拷贝失败')

    def tearDown(self):
        self.log.info('-----this is teardown-----')
        step_txt = '-----step2: 环境清理，expect:清理成功-----'
        self.log.info(step_txt)
        self.log.info('由于后续用例依赖表，故先不删除')
        rm_cmd = f"rm -rf {self.target_path1} "
        self.log.info(rm_cmd)
        rm_res = self.pri_root.sh(rm_cmd).result()
        self.log.info(rm_res)
        self.assertEqual(rm_res, '', '文件删除失败' + step_txt)
        self.log.info(f'----- {os.path.basename(__file__)} end-----')
